﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Golos.Helper;
using Golos.Http;
using Golos.Users.UserCache;

namespace Golos.Users.Http
{
    public partial class UserCacheServer : HttpServer
    {
        private void RegisterUser(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if (context.Request.QueryString.AllKeys.Contains("idtype") && context.Request.QueryString.AllKeys.Contains("indid"))
                {
                    UserIdType idType = (UserIdType) (Convert.ToInt32(context.Request.QueryString["idtype"]));
                    string indId = context.Request.QueryString["indid"];
                    string name = string.Empty;
                    string email = string.Empty;
                    if (context.Request.QueryString.AllKeys.Contains("name"))
                        name = Encoding.UTF8.GetString(Convert.FromBase64String(context.Request.QueryString["name"]));
                    if (context.Request.QueryString.AllKeys.Contains("email"))
                        email = Encoding.UTF8.GetString(Convert.FromBase64String(context.Request.QueryString["email"]));
                    Guid userId = userCache.RegisterUser(name, email, idType, indId);
                    byte[] data = userId.ToByteArray();
                    context.Response.ContentLength64 = data.LongLength;
                    context.Response.ContentEncoding = Encoding.Default;
                    context.Response.OutputStream.Write(data, 0, data.Length);
                    statusCode = (int)HttpStatusCode.OK;
                    return;
                }
                statusCode = (int)HttpStatusCode.BadRequest;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
            
        }

        private void AttachUser(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if (context.Request.QueryString.AllKeys.Contains("userid") &&
                    context.Request.QueryString.AllKeys.Contains("idtype") && 
                    context.Request.QueryString.AllKeys.Contains("indid") &&
                    context.Request.QueryString.AllKeys.Contains("saveactivity"))
                {
                    Guid userId = new Guid(context.Request.QueryString["userid"]);
                    UserIdType idType = (UserIdType)(Convert.ToInt32(context.Request.QueryString["idtype"]));
                    string indId = context.Request.QueryString["indid"];
                    bool saveActivityToSocialWeb = Convert.ToBoolean(context.Request.QueryString["saveactivity"]);
                    userCache.AttachUserIdentification(userId, idType, indId, saveActivityToSocialWeb);
                    statusCode = (int)HttpStatusCode.OK;
                    return;
                }
                statusCode = (int)HttpStatusCode.BadRequest;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }

        private void UpdateProfile(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if (context.Request.InputStream == null)
                {
                    statusCode = (int)HttpStatusCode.BadRequest;
                    return;
                }
                UserInfo userInfo = UserInfo.Deserialize(context.Request.InputStream);
                userCache.UpdateProfile(userInfo);
                statusCode = (int)HttpStatusCode.OK;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }
    }
}
